﻿using FunnyArts.JSAdapter.Attri;
using FunnyArts.JSAdapter.Inter;
using FunnyArts.PrettyClient.LocalDb.Entities;

namespace FunnyArts.Plugin.HisHcApi
{
    [JSObject(Name = "CacheMgr")]
    public class HisHcApiCacheMgr : IJavaScriptObject
    {
        [JSFunction]
        public string get(string request)
        {
            using (LocalContext localDbContext = new LocalContext())
            {
                return localDbContext.CacheMgrEntities.Where(w => w.Key == request).FirstOrDefault()?.Value;
            }
        }

        [JSFunction]
        public string set(string key, string value,int leave)
        {
            using (LocalContext localDbContext =new LocalContext())
            {
                var cacheMgr=localDbContext.CacheMgrEntities.Where(w => w.Key == key).FirstOrDefault();
                if(cacheMgr!=null) return cacheMgr.Value;
                CacheMgrEntity cacheMgrEntity = new CacheMgrEntity()
                {
                    Id=Guid.NewGuid().ToString(),
                    Key = key,
                    Value = value,
                };
                localDbContext.CacheMgrEntities.Add(cacheMgrEntity);
                localDbContext.SaveChanges();
                return cacheMgrEntity.Value;
            }
        }
    }
}
